package sibu.cn.mydemo.charts.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 刘红亮 on 15-11-13 上午9:16.
 */
public class MyLineChartView extends View {

    int width;//控件的宽
    int height;//控件的高
    Paint paint = new Paint();//画坐标轴的画笔
    Paint paintAxiData = new Paint();//画坐标轴的数据
    float axiY[] = new float[4];
    float axix[] = new float[4];
    float coordinates[][] = new float[2][3];//第一行是纵坐标的数据，[0][0]是顶部[0][1]是中部以此类推,，第二行是横坐标的数据
    String TopY = "";
    String CenterY = "";
    String BottomY = "";
    float MARGINTOP = 5.0f;
    float MARGINBOTTOM = 30.0f;
    int MARGINLEFT = 5;
    int MARGINRIGHT = 15;

    List<Float> listY = new ArrayList<>();//数据pointY
    List<Float> listX = new ArrayList<>();//数据pointX

    int position = 0;//记录以画了的最新点坐标在list中的角标位置
    // 贝赛尔曲线成员变量(起始点，控制（操作点），终止点，3点坐标)
    private float startX = 5.0f;
    private float startY = 10.f;
    private float controlX;
    private float controlY;
    private float endX;
    private float endY;

    float hx=30.0f;//横坐标的值
    float vy = 0.0f;//纵坐标的值
    // Path
    private Path path;
    // 为了不影响主画笔，这里绘制贝赛尔曲线单独用一个新画笔
    private Paint paintQ;
    private CornerPathEffect cornerPathEffect;

    public MyLineChartView(Context context) {
        super(context);
        init();
    }

    public MyLineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyLineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        paintQ = new Paint();
        path = new Path();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paintAxiData.setAntiAlias(true);//抗剧齿
        paintQ.setAntiAlias(true);
        paintQ.setColor(Color.BLACK);
//        paint.setStrokeWidth(2.0f);
        cornerPathEffect = new CornerPathEffect(5);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("onMeasure", "widthMeasureSpec = " + widthMeasureSpec + "heightMeasureSpec = " + heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("onDraw", "onDraw");
        width = canvas.getWidth();
        height = canvas.getHeight();
        Log.e("onDraw", "width = " + width + " height = " + height);
        myDraw(canvas);
    }

    public void myDraw(Canvas canvas) {

        axiY[0] = 5.0f;
        axiY[1] = 10.0f;
        axiY[2] = 5.0f;
        axiY[3] = (float) height - 30;
        axix[0] = 5.0f;//startx
        axix[1] = (float) height - 30;//starty
        axix[2] = (float) width - 15;//endx
        axix[3] = (float) height - 30;//endy
        canvas.drawLines(axiY, paint);//画纵线
        canvas.drawLines(axix, paint);//画横线
        if (coordinates[0][0] > 0) {
            TopY = String.valueOf(coordinates[0][0]);//纵坐标的顶部
            CenterY = String.valueOf(coordinates[0][1]);//纵坐标的中部
            BottomY = String.valueOf(coordinates[0][2]);//纵坐标的底部
        }
        //画Y坐标轴上的坐标点
        paintAxiData.setTextSize(30.0f);
        paintAxiData.setColor(Color.RED);
        canvas.drawText(TopY, axiY[0] + 2, axiY[1] + 20, paintAxiData);//画顶部数据
        paintAxiData.setColor(Color.BLACK);
        canvas.drawText(CenterY, axiY[2] + 2, (axiY[3] - axiY[1]) / 2 + axiY[1], paintAxiData);//画中部数据
        paintAxiData.setColor(Color.GREEN);
        canvas.drawText(BottomY, axiY[2] + 2, axiY[3] - 5, paintAxiData);//画底部数据

        //画X坐标轴上的坐标点
        paintAxiData.setColor(Color.BLACK);
        canvas.drawText("9:30", axix[0], axix[1] + 25.0f, paintAxiData);//画左边
        canvas.drawText("11:30", (axix[3] - axix[0]) / 2 + axix[0], axix[1] + 25.0f, paintAxiData);//画中部
        canvas.drawText("15:00", axix[2] - 60.0f, axix[3] + 25.0f, paintAxiData);//画右边

        //画圆滑曲线
        if (listX.size() > 0 && listY.size() > 0) {
            initQData();
            drawQpath(canvas);
            position++;
        }
    }

    public void initQData() {//更新圆滑坐标
      /*  if (position == 0) {
            startX = listX.get(position);
            startY = listY.get(position);
            endX = listX.get(position);
            endY = listY.get(position);
        } else {
            startX =listX.get(position-1);
            startY = listY.get(position-1);
            endX = listX.get(position);
            endY = listY.get(position);
        }*/
        endX = hx;
        endY = vy;
        controlX = Math.abs((endX - startX));
        controlY = Math.abs((endY - startY));
    }


    /**
     * 绘制曲线
     *
     * @param canvas 主画布
     */
    public void drawQpath(Canvas canvas) {
        path.reset();// 重置path
        path.moveTo(10.f, 20.0f);
        float cx = (startX+endX)/2;
        float cy = (startY+endY)/2;

        path.cubicTo(startX,startY,cx,cy,endX,endY);
        //path.quadTo(startX,startY,cx,cy);
        startX = endX;
        startY = endY;
        canvas.drawPath(path,paintQ);
        Log.e("drawQpath", "listsize = "+listX.size()+"startX = " + startX + " startY = " + startY + "controlX = " + controlX + " controlY = " + controlY + " endX = " + endX + " endY = " + endY);
    }

    public void update() {
        postInvalidate();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.e("onFinishInflate", "onFinishInflate");
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.e("onLayout", "changed = " + changed + " left = " + left + " top = " + top + " right = " + right + " bottom = " + bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("onSizeChanged", "w = " + w + " h = " + h + " oldw = " + oldw + " oldh = " + oldh);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        Log.e("onTouchEvent", "event rawX = " + event.getRawX() + " rawY" + event.getRawY() + " X =" + event.getX() + " Y = " + event.getY());
        return super.onTouchEvent(event);
    }

    public void setCoordinates(float[][] coordinates) {//坐标轴
        this.coordinates = coordinates;
    }

    public void changeXY(float y, long time) {//坐标的数据值转换为屏幕坐标值
        vy = (y - coordinates[0][2]) / (coordinates[0][0] - coordinates[0][2]) * (height - MARGINBOTTOM - MARGINTOP);
        int num1 = (width - MARGINLEFT - MARGINRIGHT);
        float num2 = (num1) /(2.0f*60.0f*10.0f);
        hx += num2;
        listY.add(vy);
        listX.add(hx);
        Log.e("time", "time = " + time + " hx = " + hx + " vy = " + vy + " width = " + width + " num1 = " + num1 + " num2 = " + num2);
    }
}
